#!/usr/bin/env python3
"""简化版API测试脚本"""

import requests
import json

BASE_URL = "http://localhost:8000"

def main():
    """主函数"""
    print("="*60)
    print("病历掌上查接口服务 - API测试")
    print("="*60)

    # 测试根路径
    print("测试根路径...")
    try:
        response = requests.get(f"{BASE_URL}/")
        if response.status_code == 200:
            print("PASS - 根路径访问正常")
            print("响应:", response.json())
        else:
            print("FAIL - 根路径访问失败")
    except Exception as e:
        print("ERROR - 根路径访问异常:", e)

    # 测试健康检查
    print("\n测试健康检查...")
    try:
        response = requests.get(f"{BASE_URL}/health")
        if response.status_code == 200:
            print("PASS - 健康检查正常")
            print("响应:", response.json())
        else:
            print("FAIL - 健康检查失败")
    except Exception as e:
        print("ERROR - 健康检查异常:", e)

    # 测试Swagger文档
    print("\n测试Swagger文档...")
    try:
        response = requests.get(f"{BASE_URL}/docs")
        if response.status_code == 200 and "swagger-ui" in response.text:
            print("PASS - Swagger文档可访问")
            print("地址:", f"{BASE_URL}/docs")
        else:
            print("FAIL - Swagger文档访问失败")
    except Exception as e:
        print("ERROR - Swagger文档访问异常:", e)

    # 测试报告查询API
    print("\n测试报告查询API...")
    test_data = {
        "medical_card_number": "11009009",
        "id_card": "370101200001010001",
        "start_time": "2018-01-01 18:00:00",
        "end_time": "2018-01-02 18:00:00",
        "patient_name": "王震"
    }

    try:
        response = requests.post(
            f"{BASE_URL}/api/v1/reports/main",
            json=test_data,
            headers={"Content-Type": "application/json"}
        )

        if response.status_code == 200:
            result = response.json()
            if result.get("success"):
                print("PASS - 报告查询成功")
                reports = result['data']['report']
                print(f"找到 {len(reports)} 条报告记录")
                for i, report in enumerate(reports[:3]):
                    print(f"  报告 {i+1}: {report['item']} - {report['time']} - 医院:{report['hospital_id']}")
            else:
                print("FAIL - 报告查询业务失败:", result.get('msg'))
                print("完整响应:", result)
        else:
            print("FAIL - 报告查询HTTP失败:", response.status_code)
            print("响应:", response.text)
    except Exception as e:
        print("ERROR - 报告查询异常:", e)

    print("\n" + "="*60)
    print("测试完成！")
    print("="*60)

if __name__ == "__main__":
    main()